package com.fh.util;

import com.fh.mapper.loan.CallLoanInfoMapper;
import com.fh.mapper.loan.CallLoanOverInfoMapper;
import com.fh.model.Page;
import com.fh.service.StatisticsStrategy.impl.ActualRecoverAmountCountMoneyImpl;
import com.fh.service.StatisticsStrategy.CountMoneyStrategy;
import com.fh.service.StatisticsStrategy.impl.OverdueAmountCountMoneyImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author LiGuangYi Date: 2017/9/25 15:56
 * @description:
 */
@Component
public class CountMoneyUtil {

    @Autowired
    private CallLoanInfoMapper callLoanInfoMapper;
    
    @Autowired
    private CallLoanOverInfoMapper callLoanOverInfoMapper;

    /**
     * 做去空操作及金额统计
     *
     * @param page
     * @return
     */
    public StringBuilder getCountMoney(Page page) {
        PageData.trimPage(page);
        StringBuilder countMoney = MoneyFactory(page);
        return countMoney;
    }

    /**
     * 小型工厂,策略入口
     *
     * @param page
     * @return
     */
    public StringBuilder MoneyFactory(Page page) {
        PageData pd = page.getPd();
        CountMoneyStrategy countMoneyStrategy = null;
        PageData pageData = null;
        Integer status = Integer.valueOf((String) pd.get("status"));
        if (Objects.equals(Const.CONSTANT_STRUTS2, status) || Objects.equals(Const.CONSTANT_STRUTS4, status)) {
        	pageData = callLoanOverInfoMapper.countOverdueMoney(page);
        }else{
        	pageData = callLoanInfoMapper.countOverdueMoney(page);
        }
        if (null != pageData && 0 < pageData.size()) {
            if (Objects.equals(Const.CONSTANT_STRUTS2, status) || Objects.equals(Const.CONSTANT_STRUTS4, status)) {
                countMoneyStrategy = new CountMoneyStrategy(new ActualRecoverAmountCountMoneyImpl());
                Map<String, Object> operate = countMoneyStrategy.operate(pageData);
                return this.getCountMoneyType(operate);
            } else {
                countMoneyStrategy = new CountMoneyStrategy(new OverdueAmountCountMoneyImpl());
                Map<String, Object> operate = countMoneyStrategy.operate(pageData);
                return this.getCountMoneyType(operate);
            }
        }
        return new StringBuilder();
    }

    /**
     * 返回格式字符拼接
     *
     * @param map
     * @return
     */
    public StringBuilder getCountMoneyType(Map<String, Object> map) {
        StringBuilder sb = new StringBuilder();
        return sb.append("<span style=\"position: absolute;font-size:15px;height:45px;padding:10px;\">")
                .append(map.get("zongmsg") + ":").append("￥").append(map.get("zongjine")).append(";" + map.get("zongfaxmsg") + ":")
                .append("￥").append(map.get("zongfaxi")).append("</span>");
    }

    /**
     * 金额统计
     * <p>
     * 已弃用该方法,业务需求新方法添加策略模式,及工厂以及enumeration
     *
     * @param pageData
     * @return
     */
    @Deprecated
    public StringBuilder getCountMoney(PageData pageData) {
        StringBuilder sb = new StringBuilder();
        if (null != pageData && 0 < pageData.size()) {
            BigDecimal overdue_amount = (BigDecimal) Objects.requireNonNull(pageData.get("overdue_amount"),
                    Const.MONEY_ZERO_ZERO_ZERO_STRING);
            BigDecimal overdue_fee = (BigDecimal) Objects.requireNonNull(pageData.get("overdue_fee"),
                    Const.MONEY_ZERO_ZERO_ZERO_STRING);
            return sb.append("<span style=\"position: absolute;font-size:15px;height:45px;padding:10px;\">")
                    .append("总逾期金额：").append("￥").append(overdue_amount).append("; 总逾期罚息：")
                    .append("￥").append(overdue_fee).append("</span>");
        }
        return sb;
    }

    /**
     * 金额统计
     * <p>
     * 已弃用该方法,业务需求新方法添加策略模式,及工厂以及enumeration
     *
     * @param pageDataList
     * @return
     */
    @Deprecated
    public StringBuffer getCountMoney1(List<PageData> pageDataList) {
        BigDecimal overdueAmount1 = new BigDecimal(Const.MONEY_ZERO_ZERO_ZERO_STRING);
        BigDecimal overdueFee1 = new BigDecimal(Const.MONEY_ZERO_ZERO_ZERO_STRING);
        StringBuffer sb = null;
        DecimalFormat dft = new DecimalFormat("#,##0.00");
        /*DecimalFormat dft = new DecimalFormat("#.00");//保留小数*/
        if (null != pageDataList && 0 < pageDataList.size()) {
            for (PageData pageData : pageDataList) {
                String overdueAmount0 = pageData.get("overdue_amount") == null ? pageData.get("overdueAmount")
                        == null ? Const.MONEY_ZERO_ZERO_ZERO_STRING : pageData.get("overdueAmount").toString() :
                        pageData.get("overdue_amount").toString();
                String overdueFee0 = pageData.get("overdue_fee") == null ? pageData.get("overdueFee") == null ?
                        Const.MONEY_ZERO_ZERO_ZERO_STRING : pageData.get("overdueFee").toString() :
                        pageData.get("overdue_fee").toString();
                overdueAmount1 = new BigDecimal(overdueAmount0).add(overdueAmount1);
                overdueFee1 = new BigDecimal(overdueFee0).add(overdueFee1);
            }
            sb = new StringBuffer();
            sb.append("<span style=\"position: absolute;font-size:15px;height:45px;padding:10px;\">")
                    .append("总逾期金额：").append("￥").append(dft.format(overdueAmount1)).append("; 总逾期罚息：")
                    .append("￥").append(dft.format(overdueFee1));
        }
        return sb;
    }
}
